<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class AuctionController extends BaseController
{
    /**
     * @api {get} auction/content 拍卖内容
     * @apiVersion 0.1.0
     * @apiName 拍卖内容
     * @apiGroup 拍卖房
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 1,
     * "name": "姐妹",
     * "sort": 0,
     * "add_time": 1609741068,
     * "type": 1
     * },
     * {
     * "id": 2,
     * "name": "CP",
     * "sort": 0,
     * "add_time": 0,
     * "type": 1
     * }
     * ]
     * }
     */
    public function content(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_auction')->getContentList($page,$perPage);

        return Response::sendData($list);
    }

    /**
     * @api {post} auction/content 添加或修改拍卖内容
     * @apiVersion 0.1.0
     * @apiName 添加或修改拍卖内容
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   不传是添加,传了是编辑,非必传
     * @apiParam {string} name   名称,非必传
     * @apiParam {numberic} sort  排序,非必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function updateContent(Request $request)
    {
        $data = $request->only(['id', 'name', 'sort']);
        $id = intval($request->input('id', 0));
        if (!isset($data['name'])) {
            throw new BasicException(0, '拍卖内容不能为空');
        }
        $length = mb_strlen($data['name']);
        if ($length < 1 || $length > 5) {
            // throw new BasicException(0, '拍卖内容必须为1-5个字符');
        }

        $addData['auction_content'] = $data['name'];
        $addData['create_time'] = date("Y-m-d H:i:s");
        $addData['sort'] = $data['sort'] ?? 0;

        if ($id > 0) {
            $where_check[] = ['id', '<>', $id];
            $where_check[] = ['auction_content', $data['name']];
            if (app('model_auction_content')->where($where_check)->count() > 0) {
                throw new BasicException(0, '拍卖内容已存在');
            }

            app('repo_auction')->update('model_auction_content', $id, $addData);
        } else {
            if (app('model_auction_content')->where('auction_content', $data['name'])->count() > 0) {
                throw new BasicException(0, '拍卖内容已存在');
            }

            app('repo_auction')->add('model_auction_content', $addData);
        }

        return Response::sendData('success');
    }

    /**
     * @api {delete} auction/content 删除拍卖内容
     * @apiVersion 0.1.0
     * @apiName 删除拍卖内容
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function deleteContent(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择要删除的内容');
        }
        app('repo_auction')->delete('model_auction_content', $id);

        return Response::sendData('success');
    }

    /**
     * @api {get} auction/gift 拍卖礼物
     * @apiVersion 0.1.0
     * @apiName 拍卖礼物
     * @apiGroup 拍卖房
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 1,
     * "name": "姐妹",
     * "sort": 0,
     * "add_time": 1609741068,
     * "type": 1
     * },
     * {
     * "id": 2,
     * "name": "CP",
     * "sort": 0,
     * "add_time": 0,
     * "type": 1
     * }
     * ]
     * }
     */
    public function gift()
    {
        $list = app('repo_auction')->getList('model_auction_gift');

        return Response::sendData($list);
    }

    /**
     * @api {post} auction/gift 添加或修改拍卖礼物
     * @apiVersion 0.1.0
     * @apiName 添加或修改拍卖礼物
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   不传是添加,传了是编辑,非必传
     * @apiParam {numeric} gift_id   礼物id,必传
     * @apiParam {string} gift_name   礼物名称,必传
     * @apiParam {string} gift_picture   礼物图片,必传
     * @apiParam {string} gift_special   礼物特效,必传
     * @apiParam {string} gift_price   礼物价格,必传
     * @apiParam {numberic} sort  排序,非必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function updateGift(Request $request)
    {
        $data = $request->only(['gift_id', 'gift_name', 'gift_picture', 'gift_special', 'gift_price', 'sort']);
        $id = intval($request->input('id', 0));
        if (!isset($data['gift_id']) || !$data['gift_id']) {
            throw new BasicException(0, '请选择礼物');
        }
        if (!isset($data['gift_name']) || !$data['gift_name']) {
            throw new BasicException(0, '礼物名称不能为空');
        }
        if (!isset($data['gift_picture']) || !$data['gift_picture']) {
            throw new BasicException(0, '礼物图片不能为空');
        }
        if (!isset($data['gift_price']) || $data['gift_price'] < 0) {
            throw new BasicException(0, '礼物价格不能小于0');
        }

        if ($id > 0) {
            app('repo_auction')->update('model_auction_gift', $id, $data);
        } else {
            if (app('model_auction_gift')->where('gift_id', $data['gift_id'])->count() > 0) {
                throw new BasicException(0, '拍卖礼物已存在');
            }
            app('repo_auction')->add('model_auction_gift', $data);
        }

        return Response::sendData('success');
    }

    /**
     * @api {delete} auction/gift 删除拍卖礼物
     * @apiVersion 0.1.0
     * @apiName 删除拍卖礼物
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function deleteGift(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择要删除的礼物');
        }
        app('repo_auction')->delete('model_auction_gift', $id);

        return Response::sendData('success');
    }

    /**
     * @api {get} auction/time 拍卖时间
     * @apiVersion 0.1.0
     * @apiName 拍卖时间
     * @apiGroup 拍卖房
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 1,
     * "name": "不限",
     * "total_minute": 0,
     * "add_time": 0,
     * "sort": 1
     * }
     * ]
     * }
     */
    public function time()
    {
        $list = app('repo_auction')->getList('model_auction_time');

        return Response::sendData($list);
    }

    /**
     * @api {post} auction/time 添加或修改拍卖时间
     * @apiVersion 0.1.0
     * @apiName 添加或修改拍卖时间
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   不传是添加,传了是编辑,非必传
     * @apiParam {string} name   名称,必传
     * @apiParam {string} total_minute   分钟数,必传
     * @apiParam {numberic} sort  排序,非必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function updatetime(Request $request)
    {
        $data = $request->only(['name', 'total_minute', 'sort']);
        $id = intval($request->input('id', 0));
        if (!isset($data['name']) || !$data['name']) {
            throw new BasicException(0, '名称不能为空');
        }
        if (!isset($data['total_minute']) || $data['total_minute'] < 0) {
            throw new BasicException(0, '总分钟数不能小于0');
        }

        if ($id > 0) {
            app('repo_auction')->update('model_auction_time', $id, $data);
        } else {
            app('repo_auction')->add('model_auction_time', $data);
        }

        return Response::sendData('success');
    }

    /**
     * @api {delete} auction/time 删除拍卖时间
     * @apiVersion 0.1.0
     * @apiName 删除拍卖时间
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} id   必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function deleteTime(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择要删除的礼物');
        }
        app('repo_auction')->delete('model_auction_time', $id);

        return Response::sendData('success');
    }

    /**
     * @api {get} auction/record 竞拍记录
     * @apiVersion 0.1.0
     * @apiName 竞拍记录
     * @apiGroup 拍卖房
     *
     * @apiParam {numeric} page   非必传
     * @apiParam {numeric} perPage   非必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "user_code": 130134,
     * "head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/default_avatar.png",
     * "room_code": 2395826,
     * "room_name": "",
     * "content": "cp",
     * "gift_price": 100,
     * "gift_picture": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/6007df5896870.png",
     * "current_cost 当前出价": 100,
     * "total_cost 当前总价": 200,
     * "number 出价礼物数量": 1,
     * "add_time": "2021-02-01 11:17:04",
     * "is_final 是否最终出价": 0
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": "1",
     * "next": "http://www.na.com/auction/record?=2",
     * "total": 3198
     * }
     * }
     * }
     */
    public function record(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_auction')->getRecord($request->all(), $page, $perPage);

        return Response::sendList($list);
    }

    /**
     * 拍卖信息列表
     * @param  Request  $request
     * @return string
     */
    public function result(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_auction')->getLog($request->all(), $page, $perPage);

        return Response::sendList($list);
    }

    public function auctionLog(Request $request)
    {
        $id = $request->input('id', 1);
        $list = app('repo_auction')->getAuctionDetail($id);

        return Response::sendData($list);
    }
}
